﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApp_Session
{
    public class SessionModule : IHttpModule
    {
        public void Dispose()
        {

        }


        public void Init(HttpApplication context)
        {
            //如不设置增加SessionID的过期时间，则浏览器一关闭，再次打开浏览器，Session中存储的值将会丢失。
            //Session中的数据是 依赖于 SessionID的，Cookie中的数据是依赖 过期时间/Domain/Path的。
            context.AcquireRequestState += Context_AcquireRequestState;
        }

        public static string SESSIONID = "sid";
        private void Context_AcquireRequestState(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            var cookie = app.Request.Cookies.Get(SESSIONID);
            if (cookie != null)
            {
                cookie.Expires = DateTime.Now.AddDays(30);               
                app.Response.SetCookie(cookie);
            }
        }
    }
}